﻿Ext.define('app.system.system_role_auth.DocumentAuth', {
    extend: 'Ext.panel.Panel',
    layout: 'border',
    roleColumn: [],
    initComponent: function () {
        var me = this;
        me.items = [me.getRoleGrid(), me.getPermissionGrid()];
        me.callParent();
    },
    getRoleGrid: function () {
        var me = this;
        if (!this.roleGrid) {
            this.roleGrid = new Ext.grid.Panel({
                title: localize('RoleList'),
                region: 'west',
                split: true,
                width: 300,
                border: false,
                viewConfig: {
                    enableTextSelection: true //可以复制单元格文字
                },
                forceFit: true,
                store: Ext.create("app.ux.PrmStore", {
                    url: '/api/services/app/role/GetRoles', rootProperty: 'result.items', remoteSort: false,
                    sorters: [{
                        //先按年龄降序  
                        property: 'name',
                        direction: 'DESC'
                    }],
                }),
                columns: [
                    { header: localize('RoleName'), dataIndex: 'name' },
                    { header: localize('DisplayName'), dataIndex: 'displayName' },
                    {
                        header: localize('CreateOn'), dataIndex: 'creationTime', renderer: function (value, p, record) {
                            return Ext.util.Format.date(value, 'Y-m-d');
                        }
                    }
                ],
                listeners: {
                    select: function (grid, record, index, eOpts) {
                        me.getPermissionGrid().store.load({ params: { roleId: record.data.id } });
                    }
                }
            });
        }
        return this.roleGrid;
    },
    getPermissionGrid: function () {
        var me = this;
        if (!this.permissionGrid) {
            this.permissionGrid = new Ext.grid.Panel({
                forceFit: false,
                enableLocking: true,
                title: localize('DocumentTypePermission'),
                tbar: [
                    { text: localize('Save'), icon: '/Content/images/page_save.png', handler: function () { me.save(); } },
                    "-",
                    {
                        text: localize('SelectAll'), icon: '/Content/images/accept.png', handler: function () {
                            var store = me.permissionGrid.store;
                            for (var i = 0; i < store.getCount() ; i++) {
                                var record = store.getAt(i);
                                for (var j = 0; j < me.roleColumn.length; j++) {
                                    var c = me.roleColumn[j];
                                    record.data[Ext.util.Format.lowercase(c.name)] = true;
                                }
                                //record.data.hasAuth = true;
                                record.commit();
                            }
                        }
                    },
                    "-",
                    {
                        text: localize('UnselectAll'), icon: '/Content/images/cancel.png', handler: function () {
                            var store = me.permissionGrid.store;
                            for (var i = 0; i < store.getCount() ; i++) {
                                var record = store.getAt(i);
                                for (var j = 0; j < me.roleColumn.length; j++) {
                                    var c = me.roleColumn[j];
                                    record.data[Ext.util.Format.lowercase(c.name)] = false;
                                }
                                // record.data.hasAuth = true;
                                record.commit();
                            }
                        }
                    }
                ],
                region: 'center',
                border: false,
                viewConfig: {
                    enableTextSelection: true //可以复制单元格文字
                },
                store: [],
                columns: []
            });

            me.store = Ext.create("app.ux.PrmStore", { url: '/api/services/app/role/GetDocumentPermission', autoLoad: false });
            me.store.load({ params: { roleId: 0 } });


            var _columns = [{ header: localize('DocumentPermission'), dataIndex: 'documentAuth', locked: true }];

            App.AjaxGet('/api/services/app/document/GetDocumentReportColumn', function (data) {
                for (var i = 0; i < data.result.length; i++) {
                    var root = data.result[i];
                    var rootOjb = { xtype: 'checkcolumn', header: root.text };
                    if (root.leaf) {
                        rootOjb.dataIndex = Ext.util.Format.lowercase(root.text + root.id);
                        me.roleColumn.push({ name: Ext.util.Format.lowercase(root.text + root.id) });
                    }
                    else {
                        rootOjb.columns = [];
                        for (var j = 0; j < root.children.length; j++) {
                            var record = root.children[j];
                            var twoOjb = { xtype: 'checkcolumn', header: record.text };
                            if (record.leaf) {
                                twoOjb.dataIndex = Ext.util.Format.lowercase(record.text + record.id);
                                me.roleColumn.push({ name: Ext.util.Format.lowercase(record.text + record.id) });
                            }
                            else {
                                twoOjb.columns = [];
                                for (var k = 0; k < record.children.length; k++) {
                                    var last = record.children[k];
                                    var lastObj = { xtype: 'checkcolumn', header: last.text, dataIndex: Ext.util.Format.lowercase(last.text + last.id), align: 'center' };
                                    me.roleColumn.push({ name: Ext.util.Format.lowercase(last.text + last.id) });
                                    twoOjb.columns.push(lastObj);
                                }
                            }
                            rootOjb.columns.push(twoOjb);
                        }
                    }
                    _columns.push(rootOjb);
                }
                me.permissionGrid.reconfigure(me.store, _columns);
            });


            //App.AjaxGet('/api/services/app/documentType/GetAll', function (data) {
            //    me.roleColumn = data.result;
            //    var _columns = [{ header: localize('DocumentPermission'), dataIndex: 'documentAuth' }];
            //    for (var i = 0; i < data.result.length; i++) {
            //        var record = data.result[i];
            //        _columns.push({ xtype: 'checkcolumn', sortable: false, header: record.name, dataIndex: Ext.util.Format.lowercase(record.name) });
            //    }
            //    me.permissionGrid.reconfigure(me.store, _columns);
            //});
        }
        return this.permissionGrid;
    },
    save: function () {
        var me = this;
        var sel = me.getRoleGrid().getSelection();
        if (sel.length == 0) {
            Ext.Msg.alert('系统提示', '请选择授权角色');
            return false;
        }
        var roleId = sel[0].data.id;

        var arrys = [];
        var store = me.getPermissionGrid().store;
        for (var i = 0; i < store.getCount() ; i++) {
            var _record = store.getAt(i);
            arrys.push(_record.data);
        }
        App.AjaxPost('/api/services/app/role/SetDocumentPermission', { RoleId: roleId, records: arrys }, function () {
            store.load({ params: { roleId: roleId } });
        });

    }
});